package com.ruoyi.system.outto.goods.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.V2.gameAttr.mapper.GameAttributeMapper;
import com.ruoyi.system.V2.gameAttr.service.IGameAttributeService;
import com.ruoyi.system.V2.goodInfo.mapper.GoodsInfoImgMapper;
import com.ruoyi.system.V2.inter.mapper.ArtRecMapper;
import com.ruoyi.system.model.domain.ArtRec;
import com.ruoyi.system.model.domain.GameAttribute;
import com.ruoyi.system.model.domain.GoodsInfoImg;
import com.ruoyi.system.model.domain.SalePartner;
import com.ruoyi.system.model.dto.GoodsInfoAndAttrValueDto;
import com.ruoyi.system.outto.core.LCConstant;
import com.ruoyi.system.outto.core.MapUntils;
import com.ruoyi.system.outto.game.CommonApi;
import com.ruoyi.system.outto.goods.PANGXIEApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.security.MessageDigest;
import java.util.*;

/**
 * @author lz
 * @version 1.0
 * @date 2022/6/20 14:07
 */
@Slf4j
@Service
public class PPANGXIEApiImpl implements PANGXIEApi {

    @Resource
    private GoodsInfoImgMapper goodsInfoImgMapper;

    @Resource
    private IGameAttributeService gameAttributeService;

    @Resource
    private ArtRecMapper artRecMapper;
    @Resource
    private GameAttributeMapper gameAttributeMapper;

    @Autowired
    private CommonApi commonApi;
    @Value("${desc.title}")
    private String descTitle;
    private SalePartner salePartner;

    public static void main(String[] args) {
        String apiKey = "6a112ce0db7a07104e3ad9fa16d048a5";
        Map<String, Object> params = new HashMap<>();
        params.put("type", "add");
        params.put("product_id", "123");
        Map<String, Object> data = new TreeMap<>();
        data.put("name", "测试");
        data.put("price", "100");
        data.put("content", "内容");
        data.put("image", "图片");
        data.put("daqu", "1");
        data.put("shuliang", "10");
        long time = new Date().getTime();
        data.put("add_time", time);
        data.put("fangchenmi", "0");


        data.put("shiming", "0");
        data.put("apiKey", apiKey);

        StringBuffer resut = new StringBuffer();
        for (String key : data.keySet()) {
            System.out.println("key= " + key + " and value= " + data.get(key));
            if (!"content".equals(key)) {
                resut.append(data.get(key));
            }
        }
        System.out.println(resut);
        String sign = DigestUtils.sha1Hex(resut.toString());

        params.put("data", data);
        params.put("sign", sign);
        String shangjia_id = "zyl";
        params.put("shangjia_id", shangjia_id);
        params.put("unique_no_shangjia", "1111");
        String paramsStr = JSONObject.toJSONString(params);
        String url = "http://api-shangjia.pxb7.com/api/product_wzry_v2.php";
        System.out.println("请求参数=" + paramsStr);
        String res = HttpUtils.sendPost(url, paramsStr, "application/json");
        System.out.println("返回参数=" + res);
        String message = JSONObject.parseObject(res).getString("message");
        String s = MapUntils.unicodeToString(message);
        System.out.println("返回参数=" + s);
    }


    @Override
    public AjaxResult addGame(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner salePartner) {
        this.salePartner = salePartner;
        Map<String, Object> params = params(goodsInfoAndAttrValueDto, "add", salePartner);
        log.info("螃蟹添加接口请求参数={}", JSONObject.toJSONString(params));
        String response = HttpUtils.sendPost(salePartner.getTwoOther(), JSONObject.toJSONString(params), "application/json");
        log.info("螃蟹添加接口返回参数={}", response);
        if (response.equals("[10001,\"账号已存在，请勿重复对接\"]")||response.equals("[]")) {
            throw new GlobalException(response);
        }
        JSONObject resultJson = JSONObject.parseObject(response);
        String message = resultJson.getString("message");
        message = MapUntils.unicodeToString(message);

        Map<String, Object> result = new HashMap<>();
        if (!"200".equals(resultJson.getString("code"))) {
            result.put("code", 500);
        } else {
            result.put("code", 200);
            ArtRec artRecreq = new ArtRec();
            artRecreq.setConfig(salePartner.getId());
            artRecreq.setAccountId(goodsInfoAndAttrValueDto.getId());
            List<ArtRec> artRecs = artRecMapper.selectArtRecList(artRecreq);
            if (CollectionUtils.isNotEmpty(artRecs)) {
                artRecs.get(0).setOutid(String.valueOf(goodsInfoAndAttrValueDto.getId()));
                artRecMapper.updateArtRec(artRecs.get(0));
            } else {
                artRecreq.setAccountId(goodsInfoAndAttrValueDto.getId());
                artRecreq.setCreateId(goodsInfoAndAttrValueDto.getCreateId());
                artRecreq.setType(LCConstant.interAdd);
                artRecreq.setConfig(salePartner.getId());
                artRecreq.setSaleCode(salePartner.getSaleName());
                artRecreq.setOutid(String.valueOf(goodsInfoAndAttrValueDto.getId()));
                artRecMapper.insertArtRec(artRecreq);
            }
        }
        result.put("req", JSONObject.toJSONString(params));
        result.put("res", response);
        result.put("msg", message);
        return AjaxResult.success(result);

    }

    @Override
    public AjaxResult editGame(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto,
                               SalePartner salePartner) {
        this.salePartner = salePartner;
        Map<String, Object> params = params(goodsInfoAndAttrValueDto, "update", salePartner);
        log.info("螃蟹编辑接口请求参数={}", JSONObject.toJSONString(params));
        String response = HttpUtils.sendPost(salePartner.getTwoOther(), JSONObject.toJSONString(params), "application/json");
        log.info("螃蟹编辑接口返回参数={}", response);
        JSONObject resultJson = JSONObject.parseObject(response);
        String message = resultJson.getString("message");
        message = MapUntils.unicodeToString(message);
        Map<String, Object> result = new HashMap<>();
        if (!"200".equals(resultJson.getString("code"))) {
            result.put("code", 500);
        } else {
            result.put("code", 200);
//            ArtRec artRecreq = new ArtRec();
//            artRecreq.setConfig(salePartner.getId());
//            artRecreq.setAccountId(goodsInfoAndAttrValueDto.getId());
//            List<ArtRec> artRecs = artRecMapper.selectArtRecList(artRecreq);
//            if(artRecs.size()>0) {
//                artRecMapper.deleteArtRecById(artRecs.get(0).getId());
//            }
        }
        result.put("req", JSONObject.toJSONString(params));
        result.put("res", response);
        result.put("msg", message);
        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult delGame(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner
            salePartner) {
        if (null != goodsInfoAndAttrValueDto) {
            Map<String, Object> params = new HashMap<>();
            Map<String, Object> action = new TreeMap<>();
            params.put("type", "delete");
            action.put("type", "delete");
            Map<String, Object> data = new TreeMap<>();
            data.put("product_id", goodsInfoAndAttrValueDto.getId());
            params.put("data", data);
            action.put("merchant_number", salePartner.getSaleKey());
            params.put("merchant_number", salePartner.getSaleKey());
            if (goodsInfoAndAttrValueDto.getGameName().contains("王者荣耀")) {
                params.put("game_channel", "100013");
                action.put("game_channel", "100013");
            } else if (goodsInfoAndAttrValueDto.getGameName().contains("和平精英")) {
                params.put("game_channel", "100011");
                action.put("game_channel", "100011");
            } else if (goodsInfoAndAttrValueDto.getGameName().contains("英雄联盟")) {
                params.put("game_channel", "100012");
                action.put("game_channel", "100012");
            } else if (goodsInfoAndAttrValueDto.getGameName().contains("原神")) {
                params.put("game_channel", "100026");
                action.put("game_channel", "100026");
            } else if (goodsInfoAndAttrValueDto.getGameName().contains("穿越火线")) {
                params.put("game_channel", "100039");
                action.put("game_channel", "100039");
            } else if (goodsInfoAndAttrValueDto.getGameName().contains("枪战王者")) {
                params.put("game_channel", "100033");
                action.put("game_channel", "100033");
            } else if (goodsInfoAndAttrValueDto.getGameName().contains("蛋仔派对")) {
                params.put("game_channel", "100142");
                action.put("game_channel", "100142");
            } else if (goodsInfoAndAttrValueDto.getGameName().contains("火影忍者")) {
                params.put("game_channel", "100032");
                action.put("game_channel", "100032");
            } else if (goodsInfoAndAttrValueDto.getGameName().contains("地下城与勇士")) {
                params.put("game_channel", "100025");
                action.put("game_channel", "100025");
            }
            action.put("apikey", DigestUtils.md5Hex(salePartner.getSaleKey()));
            String cover = MapUntils.getCover(action);
            cover = cover.substring(0, cover.length() - 1);
            String sign = DigestUtils.md5Hex(DigestUtils.sha1Hex(cover));
            params.put("signature", sign);
            log.info("螃蟹删除接口请求参数={}", JSONObject.toJSONString(params));
            String response = HttpUtils.sendPost(salePartner.getTwoOther(), JSONObject.toJSONString(params), "application/json");
            log.info("螃蟹删除接口返回参数={}", response);
            JSONObject resultJson = JSONObject.parseObject(response);
            String message = resultJson.getString("message");
            message = MapUntils.unicodeToString(message);
            Map<String, Object> result = new HashMap<>();
            if (!"200".equals(resultJson.getString("code"))) {
                result.put("code", 500);
            } else {
                result.put("code", 200);
                ArtRec artRecreq = new ArtRec();
                artRecreq.setConfig(salePartner.getId());
                artRecreq.setAccountId(goodsInfoAndAttrValueDto.getId());
                List<ArtRec> artRecs = artRecMapper.selectArtRecList(artRecreq);
                if(CollectionUtils.isNotEmpty(artRecs)) {
                    artRecMapper.deleteArtRecById(artRecs.get(0).getId());
                }
            }
            result.put("req", JSONObject.toJSONString(params));
            result.put("res", response);
            result.put("msg", message);
            return AjaxResult.success(result);
        } else {
            throw new GlobalException("删除ID未查询到此数据");
        }
    }

    private Map<String, Object> params(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, String type, SalePartner sa) {
        GameAttribute gameAttribute = null;
        if (goodsInfoAndAttrValueDto.getGameName().contains("王者荣耀")) {
            if (StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF16())) {
                throw new GlobalException("实名不能为空");
            }
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF16()));
        } else if (goodsInfoAndAttrValueDto.getGameName().contains("和平精英")) {
            if (StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF10())) {
                throw new GlobalException("实名不能为空");
            }
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF10()));
        } else if (goodsInfoAndAttrValueDto.getGameName().contains("原神")) {
            if (StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF20())) {
                throw new GlobalException("实名不能为空");
            }
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF20()));
        } else if (goodsInfoAndAttrValueDto.getGameName().contains("穿越火线")) {
            if (StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF3())) {
                throw new GlobalException("实名不能为空");
            }
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF3()));
        } else if (goodsInfoAndAttrValueDto.getGameName().contains("光遇")) {//=================================未配置游戏属性
            if (StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF20())) {
                throw new GlobalException("实名不能为空");
            }
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF20()));
        } else if (goodsInfoAndAttrValueDto.getGameName().contains("英雄联盟")) {
            if (StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF1())) {
                throw new GlobalException("实名不能为空");
            }
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF1()));
        } else if (goodsInfoAndAttrValueDto.getGameName().contains("火影忍者")) {
            if (StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF7())) {
                throw new GlobalException("实名不能为空");
            }
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF7()));
        } else if (goodsInfoAndAttrValueDto.getGameName().contains("枪战王者")) {
            if (StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF10())) {
                throw new GlobalException("实名不能为空");
            }
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF10()));
        } else if (goodsInfoAndAttrValueDto.getGameName().contains("地下城与勇士")) {
            if (StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF1())) {
                throw new GlobalException("实名不能为空");
            }
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF1()));
        } else {
            throw new GlobalException("当前游戏暂不支持");
        }
        Map<String, Object> params = new HashMap<>();
        Map<String, Object> action = new TreeMap<>();
        action.put("type", type);
        params.put("merchant_number", sa.getSaleKey());
        params.put("type", type);
        action.put("merchant_number", sa.getSaleKey());
        if (goodsInfoAndAttrValueDto.getGameName().contains("王者荣耀")) {
            params.put("game_channel", "100013");
            action.put("game_channel", "100013");
        } else if (goodsInfoAndAttrValueDto.getGameName().contains("和平精英")) {
            params.put("game_channel", "100011");
            action.put("game_channel", "100011");
        } else if (goodsInfoAndAttrValueDto.getGameName().contains("英雄联盟")) {
            params.put("game_channel", "100012");
            action.put("game_channel", "100012");
        } else if (goodsInfoAndAttrValueDto.getGameName().contains("原神")) {
            params.put("game_channel", "100026");
            action.put("game_channel", "100026");
        } else if (goodsInfoAndAttrValueDto.getGameName().contains("穿越火线")) {
            params.put("game_channel", "100039");
            action.put("game_channel", "100039");
        } else if (goodsInfoAndAttrValueDto.getGameName().contains("枪战王者")) {
            params.put("game_channel", "100033");
            action.put("game_channel", "100033");
        } else if (goodsInfoAndAttrValueDto.getGameName().contains("蛋仔派对")) {
            params.put("game_channel", "100142");
            action.put("game_channel", "100142");
        } else if (goodsInfoAndAttrValueDto.getGameName().contains("火影忍者")) {
            params.put("game_channel", "100032");
            action.put("game_channel", "100032");
        } else if (goodsInfoAndAttrValueDto.getGameName().contains("地下城与勇士")) {
            params.put("game_channel", "100025");
            action.put("game_channel", "100025");
        }
        action.put("apikey", DigestUtils.md5Hex(sa.getSaleKey()));
//        params.put("apikey", DigestUtils.md5Hex(sa.getSaleKey()));
        String cover = MapUntils.getCover(action);
//        cover = cover+"apikey="+;
        System.out.println("cover:::" + cover.substring(0, cover.length() - 1));
        String sign = null;
        try {
            sign = DigestUtils.md5Hex(shaEncode(cover.substring(0, cover.length() - 1)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        params.put("signature", sign);
        Map<String, Object> data = new TreeMap<>();
        String name = goodsInfoAndAttrValueDto.getTitle() == null ? goodsInfoAndAttrValueDto.getDes() : goodsInfoAndAttrValueDto.getTitle();
        data.put("name", "【" + descTitle +goodsInfoAndAttrValueDto.getId()+"】"+name.replace("+","-").replace("*","乘"));
        BigDecimal bigDecimal = commonApi.addPrice(goodsInfoAndAttrValueDto.getPrice(), sa.getSeleep());
        commonApi.setPrice(bigDecimal, goodsInfoAndAttrValueDto.getId(), sa);
        data.put("price", bigDecimal.stripTrailingZeros().toPlainString());
        data.put("image", goodsInfoAndAttrValueDto.getTitle() == null || goodsInfoAndAttrValueDto.getTitleImg().equals("") ? LCConstant.defaultImg : goodsInfoAndAttrValueDto.getTitleImg());
        StringBuffer imgs = new StringBuffer();
        GoodsInfoImg goodsInfoImg = new GoodsInfoImg();
        goodsInfoImg.setGoodsId(goodsInfoAndAttrValueDto.getId());
        goodsInfoImg.setType(Constants.STR_1);
        List<GoodsInfoImg> goodsInfoImgList = goodsInfoImgMapper.selectGoodsInfoImgList(goodsInfoImg);
        if (!CollectionUtils.isEmpty(goodsInfoImgList)) {
            for (GoodsInfoImg img : goodsInfoImgList) {
                String imgStr = img.getImgUrl() + " ";
                imgs.append("<img src=" + imgStr + "/>");
            }
        }
        String content = "【" + goodsInfoAndAttrValueDto.getDes().replace("+","-").replace("*","乘") + "】";
        data.put("content", content + imgs.toString());

        data.put("product_id", goodsInfoAndAttrValueDto.getId());
        data.put("unique_no_account", 2);
        if (goodsInfoAndAttrValueDto.getGameName().contains("和平精英")) {
            if (goodsInfoAndAttrValueDto.getSystemName().contains("安卓")) {
                if (goodsInfoAndAttrValueDto.getCarrierName().contains("微信")) {
                    data.put("system", 2);
                } else {
                    data.put("system", 1);
                }
            } else {
                if (goodsInfoAndAttrValueDto.getCarrierName().contains("微信")) {
                    data.put("system", 4);
                } else {
                    data.put("system", 3);
                }
            }
            if (gameAttribute.getFieldName().equals("可二次实名")) {
                data.put("real_name", 1);
            } else if (gameAttribute.getFieldName().equals("不可二次实名")) {
                data.put("real_name", 0);
            } else {
                data.put("real_name", 2);
            }
            data.put("fcm", 0);
        } else if (goodsInfoAndAttrValueDto.getGameName().contains("王者荣耀")) {
            if (StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF8())) {
                data.put("skin_num", goodsInfoAndAttrValueDto.getF8());
            }
            if (StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF2())) {
                GameAttribute gameAttribute1 = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF2()));
                data.put("nobility_level", Integer.parseInt(gameAttribute1.getFieldName().replace("V", "")));
            }
            if (goodsInfoAndAttrValueDto.getSystemName().contains("安卓")) {
                if (goodsInfoAndAttrValueDto.getCarrierName().contains("微信")) {
                    data.put("system", 2);
                } else {
                    data.put("system", 1);
                }
            } else {
                if (goodsInfoAndAttrValueDto.getCarrierName().contains("微信")) {
                    data.put("system", 4);
                } else {
                    data.put("system", 3);
                }
            }
            if (gameAttribute.getFieldName().equals("可二次实名")) {
                data.put("real_name", 1);
            } else if (gameAttribute.getFieldName().equals("不可二次实名")) {
                data.put("real_name", 0);
            } else {
                data.put("real_name", 2);
            }
            data.put("fcm", 0);
        } else if (goodsInfoAndAttrValueDto.getGameName().contains("英雄联盟")) {
            if (StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF4())) {
                data.put("skin_num", goodsInfoAndAttrValueDto.getF4());
            }
            if (StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF3())) {
                data.put("hero_num", goodsInfoAndAttrValueDto.getF3());
            }
            if (goodsInfoAndAttrValueDto.getGroupName().contains("电信")) {
                data.put("zone", 1);
            } else if (goodsInfoAndAttrValueDto.getGroupName().contains("网通")) {
                data.put("zone", 2);
            } else if (goodsInfoAndAttrValueDto.getGroupName().contains("峡谷之巅")) {
                data.put("zone", 3);
            } else {
                data.put("zone", 5);
            }
            String[] servers = ("7=>艾欧尼亚，8=>祖安，9=>诺克萨斯，10=>班德尔城，32=>皮尔特沃夫，33=>战争学院，42=>巨神峰，43=>雷瑟守备，44=>裁决之地，45=>黑色玫瑰，46=>暗影岛，51=>钢铁烈阳，" +
                    "53=>水晶之痕，61=>均衡教派，64=>影流，65=>守望之海，71=>征服之海，73=>卡拉曼达，75=>皮城警备，312=>云顶之巅，77=>比尔吉沃特，78=>德玛西亚，84=>弗雷尔卓德，85=>无畏先锋，" +
                    "91=>恕瑞玛，92=>扭曲丛林，93=>巨龙之巢，96=>峡谷之巅，311=>男爵领域，309=>教育网专区").split("，");
            for (String s : servers) {
                String[] strings = s.split("=>");
                if (goodsInfoAndAttrValueDto.getServerName().equals(strings[1])) {
                    data.put("server", strings[0]);
                    break;
                }
            }
            if (gameAttribute.getFieldName().contains("有修改人脸机会")) {
                data.put("real_name", 1);
            } else if (gameAttribute.getFieldName().equals("无修改人脸机会/实名是本人")) {
                data.put("real_name", 2);
            } else {
                data.put("real_name", 0);
            }
        } else if (goodsInfoAndAttrValueDto.getGameName().contains("枪战王者")) {
            if (goodsInfoAndAttrValueDto.getSystemName().contains("安卓")) {
                if (goodsInfoAndAttrValueDto.getCarrierName().contains("微信")) {
                    data.put("system", 2);
                } else {
                    data.put("system", 1);
                }
            } else {
                if (goodsInfoAndAttrValueDto.getCarrierName().contains("微信")) {
                    data.put("system", 4);
                } else {
                    data.put("system", 3);
                }
            }
            if (gameAttribute.getFieldName().contains("可修改实名")) {
                data.put("real_name", 1);
            } else if (gameAttribute.getFieldName().contains("包实名认证")) {
                data.put("real_name", 2);
            } else{
                data.put("real_name", 0);
            }
        } else if (goodsInfoAndAttrValueDto.getGameName().contains("穿越火线")) {
            if (goodsInfoAndAttrValueDto.getGroupName().equals("东部战区")) {
                data.put("zone", 1);
            } else if (goodsInfoAndAttrValueDto.getGroupName().equals("南部战区")) {
                data.put("zone", 2);
            } else if (goodsInfoAndAttrValueDto.getGroupName().equals("西部战区")) {
                data.put("zone", 3);
            } else {
                data.put("zone", 4);
            }
            String[] strings = ("33=>福建一区，34=>安徽一区，35=>上海一区，36=>上海二区，37=>浙江一区，38=>浙江二区，39=>江苏一区，40=>江苏二区，41=>教育专区，42=>移动专区，43=>江西一区，" +
                    "44=>南方大区，45=>湖南一区，46=>湖南二区，47=>湖北一区，48=>湖北二区，49=>广西一区，50=>广东一区，51=>广东二区，52=>广东三区，53=>广东四区，54=>四川一区，55=>四川二区，" +
                    "56=>重庆一区，57=>云南一区，58=>陕西一区，59=>河北一区，60=>山西一区，61=>北京一区，62=>北京二区，63=>北京三区，64=>北京四区，65=>山东一区，66=>山东二区，67=>河南一区，" +
                    "68=>河南二区，69=>吉林一区，70=>黑龙江一区，71=>辽宁一区，72=>辽宁二区，73=>辽宁三区，74=>北方大区").split("，");
            for (String s : strings) {
                String[] server = s.split("=>");
                if (goodsInfoAndAttrValueDto.getServerName().equals(server[1])) {
                    data.put("server", server[0]);
                }
            }
            data.put("rank", 1);
            if (gameAttribute.getFieldName().contains("有修改机会")) {
                data.put("real_name", 1);
            } else if (gameAttribute.getFieldName().equals("无修改机会且实名是本人")) {
                data.put("real_name", 2);
            } else {
                data.put("real_name", 0);
            }
        } else if (goodsInfoAndAttrValueDto.getGameName().contains("火影忍者")) {
//            data.put("server_id", 1);
            if (goodsInfoAndAttrValueDto.getSystemName().contains("安卓")) {
                if (goodsInfoAndAttrValueDto.getCarrierName().contains("微信")) {
                    data.put("system", 2);
                } else {
                    data.put("system", 1);
                }
            } else {
                if (goodsInfoAndAttrValueDto.getCarrierName().contains("微信")) {
                    data.put("system", 4);
                } else {
                    data.put("system", 3);
                }
            }
            if (gameAttribute.getFieldName().equals("可改二次")) {
                data.put("real_name", 1);
            } else if (gameAttribute.getFieldName().equals("不可二次")) {
                data.put("real_name", 0);
            } else {
                data.put("real_name", 2);
            }
            if (StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF3())) {
                GameAttribute gameAttribute1 = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF3()));
                data.put("level", gameAttribute1.getFieldName().replace("V", ""));
            }
        } else if (goodsInfoAndAttrValueDto.getGameName().contains("地下城与勇士")) {
            String[] areas = "1=>跨1，2=>跨2，3=>跨3A，4=>跨3B，5=>跨4，6=>跨5，7=>跨6，8=>跨7，9=>跨8".split("，");
            for (String s : areas) {
                if (goodsInfoAndAttrValueDto.getGroupName().equals(s.split("=>")[1])) {
                    data.put("area", s.split("=>")[0]);
                }
            }
            GameAttribute attribute = gameAttributeMapper.selectGameAttributeById(goodsInfoAndAttrValueDto.getServerId());
            String servicesName = getServicesName(attribute.getFieldName());
            data.put("server",servicesName);
        } else if(goodsInfoAndAttrValueDto.getGameName().contains("光遇")){
            if(goodsInfoAndAttrValueDto.getSystemName().equals("安卓系统")){
                data.put("system",1);
                data.put("channel_id",4);
            }else{
                data.put("system",2);
                data.put("channel_id",1);
            }
            data.put("login_type",0);
            data.put("fcm",0);
        }
        params.put("data", data);
        return params;
    }

    public String getServicesName(String ser) {
        String number = "";
        switch (ser) {
            case "广东1区":
            case "广东2区":
            case "广东3区":
            case "广东4区":
            case "广东5区":
            case "广东6区":
            case "广东7区":
            case "广东8区":
            case "广东9区":
            case "广东10区":
            case "广东11区":
            case "广东12区":
            case "广东13区":
                number = "1";
                break;
            case "广西1区":
            case "广西2/4区":
            case "广西3区":
            case "广西5区":
                number = "2";
                break;
            case "广州1/2区":
                number = "3";
                break;
            case "湖北1区":
            case "湖北2区":
            case "湖北3区":
            case "湖北4区":
            case "湖北5区":
            case "湖北6区":
            case "湖北7区":
            case "湖北8区":
                number = "4";
                break;
            case "湖南1区":
            case "湖南2区":
            case "湖南3区":
            case "湖南4区":
            case "湖南5区":
            case "湖南6区":
            case "湖南7区":
                number = "5";
                break;
            case "四川1区":
            case "四川2区":
            case "四川3区":
            case "四川4区":
            case "四川5区":
            case "四川6区":
                number = "6";
                break;
            case "西北1区":
            case "西北2/3区":
                number = "7";
                break;
            case "新疆1区":
                number = "8";
                break;
            case "西南1区":
            case "西南2区":
            case "西南3区":
                number = "9";
                break;
            case "陕西1区":
            case "陕西2/3区":
                number = "10";
                break;
            case "重庆1区":
            case "重庆2区":
                number = "11";
                break;
            case "云南1区":
                number = "12";
                break;
            case "贵州1区":
                number = "13";
                break;
            case "云贵1区":
                number = "14";
                break;
            case "江苏5/7区":
            case "江苏6区":
            case "江苏8区":
                number = "15";
                break;
            case "浙江4/5区":
            case "浙江6区":
            case "浙江7区":
                number = "16";
                break;
            case "上海4/5区":
                number = "17";
                break;
            case "江西3区":
                number = "18";
                break;
            case "安徽3区":
                number = "19";
                break;
            case "福建3/4区":
                number = "20";
                break;
            case "上海1区":
            case "上海2区":
            case "上海3区":
                number = "21";
                break;
            case "浙江1区":
            case "浙江2区":
            case "浙江3区":
                number = "22";
                break;
            case "江苏1区":
            case "江苏2区":
            case "江苏3区":
            case "江苏4区":
                number = "23";
                break;
            case "福建1区":
            case "福建2区":
                number = "24";
                break;
            case "安徽1区":
            case "安徽2区":
                number = "25";
                break;
            case "江西1区":
            case "江西2区":
                number = "26";
                break;
            case "山东1区":
            case "山东2/7区":
                number = "27";
                break;
            case "北京1区":
            case "北京2/4区":
                number = "28";
                break;
            case "东北1区":
                break;
            case "东北2区":
                break;
            case "东北3/7区":
                number = "29";
                break;
            case "河北1区":
                number = "30";
                break;
            case "河南1区":
            case "河南2区":
                number = "31";
                break;
            case "山西1区":
                number = "32";
                break;
            case "黑龙江1区":
                number = "33";
                break;

            case "华北1区":
            case "华北2区":
            case "华北3区":
                number = "34";
                break;
            case "吉林1/2区":
                number = "35";
                break;
            case "辽宁1区":

                number = "36";
                break;
            case "河北2/3区":
            case "河北4区":
            case "河北5区":
                number = "37";
                break;
            case "东北4/5/6区":
                number = "38";
                break;
            case "辽宁2区":
            case "辽宁3区":
                number = "39";
                break;
            case "河南3区":
            case "河南4区":
            case "河南5区":
            case "河南6区":
            case "河南7区":
                number = "40";
                break;
            case "华北4区":
                number = "41";
                break;
            case "黑龙江2/3区":
                number = "42";
                break;
            case "山东3区":
            case "山东4区":
            case "山东5区":
            case "山东6区":
                number = "43";
                break;
            case "北京3区":
                number = "44";
                break;
            case "天津1区":
                number = "45";
                break;
            case "内蒙古1区":
                number = "46";
                break;
            case "山西2区":
                number = "47";
                break;
        }
        return number;
    }

    private String reRedioValue(String value) {
        String reStr = "";
        if (StringUtils.isNotEmpty(value)) {
            GameAttribute gameAttribute =
                    gameAttributeService.selectGameAttributeById(Long.valueOf(value));
            if (gameAttribute != null) {
                if (StringUtils.isNotEmpty(gameAttribute.getFieldName())) {
                    reStr = gameAttribute.getFieldName();
                }

            }
        }
        return reStr;
    }

    /**
     * 返回outId
     *
     * @param goodsInfoAndAttrValueDto
     * @param salePartner
     * @return
     */
    private String reOutId(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner salePartner) {
        String outId = "";
        ArtRec artRecreq = new ArtRec();
        artRecreq.setConfig(salePartner.getId());
        artRecreq.setAccountId(goodsInfoAndAttrValueDto.getId());
        List<ArtRec> artRecs = artRecMapper.selectArtRecList(artRecreq);
        if (CollectionUtils.isNotEmpty(artRecs)) {
            if (artRecs.size() == 1) {
                outId = artRecs.get(0).getOutid();
            } else {
                throw new GlobalException("商品id为 " + goodsInfoAndAttrValueDto.getId() + " 在供应商id为" + salePartner.getId() +
                        "下outId为多个,请查看");
            }
        } else {
            throw new GlobalException("商品id为 " + goodsInfoAndAttrValueDto.getId() + " 在供应商id为" + salePartner.getId() +
                    "下outId未找到,请查看");
        }
        return outId;
    }

    public static String shaEncode(String inStr) throws Exception {
        MessageDigest sha = null;
        try {
            sha = MessageDigest.getInstance("SHA");
        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
            return "";
        }

        byte[] byteArray = inStr.getBytes("UTF-8");
        byte[] md5Bytes = sha.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16) {
                hexValue.append("0");
            }
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();
    }
}


